_realname=libwebm
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.0.28
pkgrel=1
pkgdesc="WebM video file parser (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://chromium.googlesource.com/webm/libwebm"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://github.com/webmproject/${_realname}/archive/refs/tags/${_realname}-${pkgver}.tar.gz
        cmake_install.patch
        libwebm.pc)
sha256sums=('4df11d93260d3cd9f17c3697b0828d38400a8f87082183368df6a1ae7a9fc635'
            'e405b5351c3d8c69ca10af6a900df0cf99bd89415c7982b222821a4a06220859'
            '082aea8e45bf42faba18e00d00561d4b5aba307261df74c18238fcecc792cc1f')

prepare() {
  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  # modified from AUR https://aur.archlinux.org/cgit/aur.git/plain/cmake_install.patch?h=libwebm
  patch -p1 -i ${srcdir}/cmake_install.patch
}

build() {
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_WEBM_PARSER=ON \
    ../${_realname}-${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DENABLE_WEBM_PARSER=ON \
    ../${_realname}-${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  install -Dm644 LICENSE.txt "$pkgdir${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  cd ${srcdir}
  sed -i "s|@PREFIX@|${MINGW_PREFIX}|g" libwebm.pc
  sed -i "s|@VERSION@|${pkgver}|g" libwebm.pc
  install -Dm644 libwebm.pc "$pkgdir${MINGW_PREFIX}/lib/pkgconfig/libwebm.pc"
}
